<template>
  <div>
    <ul class="list">
      <li v-for="(item, index) in resultData" :key="index" >
        <div class="title">
          <span class="classify">【<i class="iconfont icon-zhuanli"></i>专利】</span>
          <h5 v-html="item['ttl']" @click="toDetail(item)"></h5>
        </div>
        <p v-html="item['abst_wordsLight']" :title="item['abst']" class="content"></p>
        <p class="other">
          <span v-show="item.an1!==''" :title="`第一申请人：${item.an1}`">{{item.an1}}</span>
          <span v-show="item.in_short5!==''" :title="`发明人：${item.inStr}`">{{item.in_short5}}</span>
          <span v-show="item.isd" :title="`公开日期：${item.isd}`">{{item.isd}}</span>
        </p>
        <!-- 收藏 -->
        <!-- <div class="collect-btn" @click="collet(item, index)">
          <span class="iconfont"
            :class="{'active': item.isCollect, 'icon-shoucang-': item.isCollect, 'icon-shoucang': !item.isCollect}"
          ></span>
          <span v-if="item.isCollect" class="label">已收藏</span>
        </div> -->
      </li>
      <!-- no-data -->
      <div v-if="noData" class="no-data">
        <loading1 :emptyText="emptyText"/>
      </div>
    </ul>
    <el-pagination
      v-if="resultData.length>0"
      @current-change="handleCurrentChange"
      layout="prev, pager, next"
      :current-page.sync="opts['ipi']"
      :page-size="opts['ips']"
      :total="total"
      class="elpagination"
    ></el-pagination>
  </div>
</template>

<script>
import {thousands, wordsLight} from '@/utils/utils'
import countryJson from '@/utils/mock/country.json'
export default {
  data() {
    return {
      opts: {},
      resultData: [],
      total: 0,
      noData: false,
      emptyText: '',
      facetParams: {}, // 聚合过来的参数
      extra: {}, // 存储额外变量
    }
  },
  methods: {
    init(opts) {
      this.opts = JSON.parse(JSON.stringify(opts))
      // 
      if (this.opts.resetFacet) {
        this.facetParams = {}
      }
      this.search()
    },
    search() {
      this.resultData = []
      this.noData = true
      this.emptyText = 'loading'
      let params = {
        ipi: this.opts.ipi,
        ips: this.opts.ips,
      }
      if (this.opts.sort !== -1) {
        params['order'] = this.opts.sort
      }
      // 聚合的参数
      if (Object.values(this.facetParams).length > 0) {
        let facetParams = JSON.parse(JSON.stringify(this.facetParams))
        if (facetParams['in2id.id']) {
          facetParams['in2id.id'] = this.extra['in2id.id'][0]['in2id.id']
        }
        params['filter'] = facetParams
      }
      // 判断是否有高级检索
      if (this.opts.advanced) {
        if (this.opts.accurateSearch !== '') {
          params['accurateSearch'] = this.opts.accurateSearch
        }
        if (this.opts.includeSearch !== '') {
          params['includeSearch'] = this.opts.includeSearch
        }
        if (this.opts.notIncludeSearch !== '') {
          params['notIncludeSearch'] = this.opts.notIncludeSearch
        }
      } else {
        if (this.opts.searchTxt !== '') {
          params['keyword'] = this.opts.searchTxt
        }
      }

      this.$post(this.$Url.IR.patentListPage, params).then(res => {
        let data = res.data
        let result = data.result || []
        if (result.length <= 0) {
          this.emptyText = '无法找到你所查询的内容，换个关键词试试'
        } else {
          this.noData = false
        }
        // 配置高亮
        let settings = [
          {words: this.opts.searchTxt, color: '#FF8C00'}
        ]
        result.forEach(v => {
          v.ttl = wordsLight(v.ttl, settings)
          if (v.abst && v.abst !== '') {
            let lastStr = v.abst[v.abst.length-1]
            let reg = /[.|。|？]/g
            if (!reg.test(lastStr)) {
              v.abst += '...'
            }
          } else {
            v.abst = '暂无内容'
          }
          v.abst_wordsLight = wordsLight(v.abst, settings)
          v.an_short5 = ''
          if (v.an && v.an.length > 0) { // 申请人
            v.anStr = v.an.join(' | ')
            let an_short5 = v.an.filter((b, j) => j<5)
            if (v.an.length > an_short5.length) {
              an_short5 = an_short5.join(' | ') + '...'
            } else {
              an_short5 = an_short5.join(' | ')
            }
            v.an_short5 = an_short5
          }
          v.in_short5 = ''
          if (v.in && v.in.length > 0) { // 发明人
            v.inStr = v.in.join(' | ')
            let in_short5 = v.in.filter((b, j) => j<5)
            if (v.in.length > in_short5.length) {
              in_short5 = in_short5.join(' | ') + '...'
            } else {
              in_short5 = in_short5.join(' | ')
            }
            v.in_short5 = in_short5
          }
        })
        this.resultData = result
        let _total = data.total || 0
        this.total = _total
        this.$emit('resultTotal', thousands(_total))
        // 聚合
        if (this.opts.resetFacet) {
          // 国家地区code码转中文
          data.nd.forEach(v => {
            let obj = countryJson.countrs.find(b => b.code === v.key)
            if (obj) {
              v.key_string = obj.name
            }
          })
          let facetData = {
            na: data.na, // 专利类型
            apd: data.apd, // 申请时间
            nd: data.nd, // 国家地区
            'in2id.id': data['in2id.id'], // 发明人
            'an1.keyword': data['an1.keyword'], // 申请人
            'lss': data['lss'], // 法律状态
          }
          this.extra['in2id.id'] = data['in2id.id'] // 发明人
          this.$Bus.$emit('facetData', facetData)
        }
      }).catch(err => {
        this.emptyText = '无法找到你所查询的内容，换个关键词试试'
      })
    },
    // 是否收藏
    isCollect(eid, sth) {
      this.$get(this.$Url.company.isCollect2, {
        eid
      }).then(res => {
        let data = res.data
        this.$set(sth, "isCollect", data);
      }).catch(err => {})
    },
    // 收藏
    collet(item, index) {
      return
      if (item.isCollect) {
        // 取消收藏
        this.$get(this.$Url.company.cancelCollectComp, {
          eid: item.eid
        }).then(res => {
          if (res.status == 200) {
            this.$message({
              type: "success",
              message: "取消收藏成功"
            });
            this.$set(this.resultData[index], "isCollect", !item.isCollect);
          } else {
            this.$message({
              type: "error",
              message: "取消收藏失败",
              duration: 1000
            });
          }
        }).catch(e => {console.log(e);});
      } else {
        this.$get(this.$Url.company.collectComp, {
          eid: item.eid
        }).then(res => {
          if (res.status == 200) {
            this.$message({
              type: "success",
              message: "收藏成功",
              duration: 1000
            });
            this.$set(this.resultData[index], "isCollect", !item.isCollect);
          } else {
            this.$message({
              type: "error",
              message: "收藏失败",
              duration: 1000
            });
          }
        }).catch(e => {console.log(e);});
      }
    },
    // 分页
    handleCurrentChange(val) {
      this.opts.ipi = val
      this.search()
    },
    // 
    toDetail(item) {
      this.$router.push({
        name: 'TechnicalValuationDetail',
        query: {
          pn: item.pn,
          active: 1
        }
      })
    },
  },
  mounted() {
    this.$Bus.$on('facetParams', params => {
      this.facetParams = params
      this.$set(this.opts, 'ipi', 1)
      this.search()
    })
  },
  beforeDestroy() {
    this.$Bus.$off('facetParams')
  }
}
</script>

<style lang="scss" scoped>
.list {
  min-height: 1.3rem;
  li {
    position: relative;
    padding-bottom: .2rem;
    border-bottom: 1px solid $color36;
    .title {
      position: relative;
      line-height: .48rem;
      padding-left: 0;
      padding-right: .6rem;
      border-bottom: 0;
      .classify {
        display: inline-block;
        min-width: .85rem;
        margin-right: .05rem;
        font-size: .14rem;
        vertical-align: middle;
        background-image: linear-gradient(50deg, $color55, $color56);
        background-clip:text;
        color: transparent;
        i {
          display: inline-block;
          padding: 0 .04rem;
        }
      }
      h5 {
        display: inline-block;
        width: calc(100% - 1.5rem);
        // font-family: SourceHanSansCN-Medium;
        font-weight: normal;
        font-size: .18rem;
        vertical-align: middle;
        color: $color57;
        cursor: pointer;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        ::v-deep {
          em {
            color: $color120;
          }
        }
      }
    }
    .content {
      display: -webkit-box;
      max-height: .96rem;
      line-height: .24rem;
      margin: .1rem 0;
      font-size: .15rem;
      color: $color58;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 4;//控制行数
      word-wrap: break-word;
      word-break: break-all;
      overflow: hidden;
      ::v-deep {
        em {
          color: $color120;
        }
      }
    }
    .other {
      position: relative;
      display: flex;
      align-items: center;
      width: 100%;
      padding-top: .04rem;
      span {
        position: relative;
        display: inline-block;
        line-height: .24rem;
        padding: 0 .06rem;
        margin-right: .1rem;
        font-size: .12rem;
        &:nth-of-type(1) {
          color: $color66;
          background-color: $color67;
        }
        &:nth-of-type(2) {
          color: $color62;
          background-color: $color63;
        }
        &:nth-of-type(3) {
          color: $color64;
          background-color: $color65;
        }
      }
    }
    .collect-btn {
      position: absolute;
      top: .14rem;
      right: 0;
      display: block;
      font-size: .16rem;
      line-height: .2rem;
      text-align: center;
      color: $color120;
      cursor: pointer;
      span {
        display: inline-block;
        vertical-align: middle;
        &.iconfont {
          font-size: .24rem;
        }
        &.label {
          margin-left: .05rem;
          font-size: .16rem;
        }
      }
      &.score {
        background-color: #8696E8;
      }
      &.el-icon-star-off {
        font-size: 18px;
        background: #909399;
        cursor: pointer;
      }
      &.active {
        font-size: 21px;
        background-color: #8696E8;
      }
    }
  }
}
.no-data {
  line-height: 1.2rem;
}
.elpagination {
  padding: .2rem 0;
  ::v-deep {
    .el-pager {
      li {
        font-weight: normal;
        &.active {
          font-size: 16px;
          font-weight: bold;
          color: $mainColor;
        }
      }
    }
    .more + .number {
      display: none;
    }
  }
}
</style>